package com.baidu.service;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.util.Base64Util;
import com.baidu.utils.AipFaceSample;
import org.json.JSONObject;

import java.util.HashMap;

// AipFaceService 人脸识别业务处理类
public class AipFaceService {

    // 优化：免实例化对象, 单例模式
    private static AipFaceService aipFaceService;
    public static AipFaceService getAipFaceService(){
        if(aipFaceService == null) aipFaceService = new AipFaceService();
        return aipFaceService;
    }

    public AipFace aipFace = AipFaceSample.getAipFace();

    // 人脸识别 faceDetect
    public String faceDetect(byte[] img){

        // Map<K, V> K: 键的类型， V: 值的类型
        HashMap<String, Object> options = new HashMap<>();
        options.put("face_field", "age,beauty,expression,face_shape,gender,glasses,quality,eye_status,emotion,face_type");
        options.put("face_type", "LIVE");
        options.put("max_face_num", "3");
        options.put("liveness_control", "LOW");

        String img_bs64 = Base64Util.encode(img);  // Base64 转码

        // 人脸识别
        JSONObject json = aipFace.detect(img_bs64, "BASE64", options);
        System.out.println(json.toString());

        return  json.toString();  // 转换为字符串
    }

}
